package hshsm.managers;

import java.util.Hashtable;

import hsh.exceptions.DeviceDriverNotFoundException;
import hsh.exceptions.DeviceOccupiedException;
import hsh.processor.Processor;
import hsh.systemcalls.SystemCalls;
import hsh.systemcalls.SystemCalls.Call;
import hsh.util.Interruption;
import hshs.process.Program;



public class InterruptManager {

	private Hashtable<Interruption,Program> interruptionVector;
	private SystemCalls systemCalls;
	
	public InterruptManager() {
		this.interruptionVector = new Hashtable<Interruption,Program>();
		buildInterruptionVector();
	   	
	}
	
	private void buildInterruptionVector(){
		//interruptionVector.put(Interruption.REQUEST_d, new Program());
	}

	public void handleInterruptation(Interruption interruption,Scheduler scheduler) {
		
		if(interruption != Interruption.TRAP)
		scheduler.addNewProcess(this.interruptionVector.get(interruption));
		else{
			SystemCalls.Call call = Processor.getInstance().getRegisterTrap();
			
			switch(call){
				case ON :
					try {
						systemCalls.on(Processor.getInstance().nextParameter());
					} catch (DeviceDriverNotFoundException e) {
						System.err.printf("%s","Device Driver Not Found");
					} catch (DeviceOccupiedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
			}
		}
	}

}
